<template>
  <div class="register">
    <div id="registerbg">
      <div class="container">
        <el-form
          :model="registerUser"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="registerForm"
        >
          <el-form-item label="手机号" prop="tel" ref="tel">
            <el-input v-model="registerUser.tel"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="registerUser.password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="password2">
            <el-input type="password" v-model="registerUser.password2"></el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="code" class="code">
            <el-input v-model="registerUser.code" placeholder="请输入验证码"></el-input>
            <div class="sendcode">
              <el-button
                type="button"
                @click="sendcode"
                :disabled="disabled"
                v-if="disabled==false"
              >发送验证码</el-button>
              <el-button
                type="button"
                @click="sendcode"
                :disabled="disabled"
                v-if="disabled==true"
              >{{btntxt}}</el-button>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "register",
  data: function() {
    //自定义验证表单元素的方法
    var validatePass2 = (rule, value, callback) => {
      if (value !== this.registerUser.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      registerUser: {
        tel: "",
        password: "",
        password2: "",
        code: ""
      },
      disabled: false,
      time: 0,
      btntxt: "重新发送",
      rules: {
        tel: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          { min: 11, max: 11, message: "手机号码必须是11位", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 6, message: "密码必须是6位", trigger: "blur" }
        ],
        password2: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { validator: validatePass2, trigger: "blur" }
        ],
        code: [
          { required: true, message: "验证码不能为空", trigger: "blur" },
          { trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    //手机发送验证码
    sendcode() {
      const reg = 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/;
      if (!reg.test(this.registerUser.tel)) {
        this.$message({
          message: "请输入正确的手机号"
          // center:true
        });
        return;
      } else {
        console.log(this.registerUser.tel);
        this.$message({
          message: "发送成功",
          type: "success"
        });
        this.time = 60;
        this.disabled = true;
        this.timer();
        this.$axios
          .get(
            "http://localhost:3000/users/getVCode?userTel=" +
              this.registerUser.tel
          )
          .then(res => {
            console.log(res);
          })
          .catch(err => {
            console.log("错误信息", err);
          });
      }
    },
    timer() {
      if (this.time > 0) {
        this.time--;
        this.btntxt = this.time + "s后重新获取";
        setTimeout(this.timer, 1000);
      } else {
        this.time = 0;
        this.btntxt = "获取验证码";
        this.disabled = false;
      }
    },
    submitForm(formName) {
      //通过ref定位到form表单
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$axios
            .post("http://localhost:3000/users/register", this.registerUser)
            .then(res => {
              console.log("注册成功");
              if (res.data.data == 0) {
                this.$message({
                  message: "注册成功",
                  type: "success"
                });
              }
              this.$router.push("/Login");
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      //实现重置表单元素数据
      this.$refs[formName].resetFields();
      this.$router.push("/index");
    }
  }
};
</script>
<style lang="css" scoped>
.register {
  width: 100%;
  height: 650px;
  padding: 0;
  background-image: url("../assets/GYQ/slider1.jpg");
  background-position: center center;
  background-size: 100% 100%;
}
#registerbg {
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.466);
}
.container {
  height: 100%;
  padding-top: 150px;
  padding-left: 55%;
}

.registerForm {
  width: 400px;
  height: 370px;
  border-radius: 10px;
  padding: 25px 50px 10px 10px;
  background-color: white;
}
.registerForm:hover {
  box-shadow: 0 0 10px 6px rgba(160, 146, 146, 0.5);
}
@media (max-width: 769px) {
  .registerForm {
    height: 100%;
    width: 100%;
    border-radius: 0px;
  }
  .container {
    height: 100%;
    padding: 0;
  }
}
.sendcode {
  position: absolute;
  right: 0;
  top: 0;
}
</style>